package com.material.demo.lyric;

import android.graphics.Paint;
import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by pc on 2016/12/22.
 */

public class LrcParser implements ILyricParser {

    private static final String TAG = LrcParser.class.getName();
    private final int CHAR_ARRAY_SIZE = 256;
    private boolean mStopReader;
    private List<LyricInfo> mLyrics;

    public LrcParser() {
    }

    @Override
    public void parser(InputStream stream) {
        if (stream != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[CHAR_ARRAY_SIZE];
            try {
                while ((len = stream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }

                String lyricContent = bos.toString();
                if(!TextUtils.isEmpty(lyricContent)){
                    String[] lyricInfos = lyricContent.split("\n");
                    if(lyricInfos!=null && lyricInfos.length>0){
                        for(String info : lyricInfos){
                            logMsg("歌詞 : " + info);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }

                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public List getLyrics() {
        return mLyrics;
    }

    private void logMsg(String msg) {
        Log.e(TAG, msg);
    }

    @Override
    public int getOffset() {
        return 0;
    }

    @Override
    public void measureTextSize(Paint paint) {

    }
}
